package cc.comac.mom.datahandlers.entity;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cc.comac.mom.bean.EquipRepairRepository;
import cc.comac.mom.dao.entity.EquipRepairRepositoryDao;
import cc.comac.mom.entity.EquipRepairRepositoryEntity;
import cc.comac.mom.util.BaseEntityDataUtil;
import cc.comac.mom.util.ResultBean;

@Component
@Transactional
public class EquipRepairRepositoryDataHandler extends ServiceImpl<EquipRepairRepositoryDao, EquipRepairRepositoryEntity>{
	@Autowired
	private EquipRepairRepositoryDao equipRepairRepositoryDao;
	@Autowired
	private BaseEntityDataUtil baseEntityDataUtil;
	
	public ResultBean save(EquipRepairRepository obj) {
		ResultBean resultBean=new ResultBean(false);
		EquipRepairRepositoryEntity entity = obj.getInnerData();
		
		long equipCategoryId = entity.getEquipCategoryId();
//		String faultCauses = entity.getFaultCauses();
		long faultCauseId = entity.getFaultCauseId();
		QueryWrapper<EquipRepairRepositoryEntity> queryWrapper = new QueryWrapper<EquipRepairRepositoryEntity>();
		queryWrapper.eq("equip_category_id", equipCategoryId);
//		queryWrapper.eq("fault_causes", faultCauses);
		queryWrapper.eq("fault_cause_id", faultCauseId);
		queryWrapper.eq("row_status", 1);
		List<EquipRepairRepositoryEntity> existsEntity = equipRepairRepositoryDao.selectList(queryWrapper);
		
		if(existsEntity!=null && existsEntity.size()>0) {
			throw new RuntimeException("数据为空");
		}
		baseEntityDataUtil.setBaseEntityData(entity);
		boolean save = this.save(entity);
		if(!save) {
			throw new RuntimeException("保存失败");
		}
		resultBean.setMessage(String.valueOf(entity.getId()));
		resultBean.setSuccess(true);
		return resultBean;
	}
	
	public ResultBean save(List<EquipRepairRepository> objs) {
		
		ResultBean resultBean=new ResultBean(false);
		List<EquipRepairRepositoryEntity> entityList = new ArrayList<>();
		for(EquipRepairRepository obj:objs) {
			EquipRepairRepositoryEntity entity = obj.getInnerData();
			
			long equipCategoryId = entity.getEquipCategoryId();
		//	String faultCauses = entity.getFaultCauses();
			long faultCauseId = entity.getFaultCauseId();
			
			QueryWrapper<EquipRepairRepositoryEntity> queryWrapper = new QueryWrapper<EquipRepairRepositoryEntity>();
			queryWrapper.eq("equip_category_id", equipCategoryId);
		//	queryWrapper.eq("fault_causes", faultCauses);
			queryWrapper.eq("fault_cause_id", faultCauseId);
			queryWrapper.eq("row_status", 1);
			List<EquipRepairRepositoryEntity> existsEntity = equipRepairRepositoryDao.selectList(queryWrapper);
			
			if(existsEntity!=null && existsEntity.size()>0) {
				throw new RuntimeException("数据为空");
			}
			baseEntityDataUtil.setBaseEntityData(entity);
			entityList.add(entity);
		}
		
		
		boolean save = this.saveBatch(entityList);
		if(!save) {
			throw new RuntimeException("保存失败");
		}
		
		resultBean.setSuccess(true);
		return resultBean;
	}
	public ResultBean update(EquipRepairRepository obj) {
		ResultBean resultBean=new ResultBean(false);
		EquipRepairRepositoryEntity entity = obj.getInnerData();
		
		long id = entity.getId();
		long equipCategoryId = entity.getEquipCategoryId();
		long faultCauseId = entity.getFaultCauseId();
//		String faultCauses = entity.getFaultCauses();
		EquipRepairRepositoryEntity existsEntity = equipRepairRepositoryDao.selectById(id);
		if(existsEntity==null) {
			throw new RuntimeException("数据为空");
		}
		
		QueryWrapper<EquipRepairRepositoryEntity> queryWrapper = new QueryWrapper<EquipRepairRepositoryEntity>();
		queryWrapper.eq("equip_category_id", equipCategoryId);
	//	queryWrapper.eq("fault_causes", faultCauses);
		queryWrapper.eq("fault_cause_id", faultCauseId);
		queryWrapper.ne("equip_repair_repository_id", id);
		List<EquipRepairRepositoryEntity> existsEntitys = equipRepairRepositoryDao.selectList(queryWrapper);
		
		if(existsEntitys!=null && existsEntitys.size()>0)  {
			resultBean.setErrorMessge("equip.repair.repository.exists");
			resultBean.setMsgParams(new String[] {equipCategoryId+"",faultCauseId+""});
			return resultBean;
		}
		baseEntityDataUtil.updateBaseEntityData(entity);
		boolean save = this.updateById(entity);
		if(!save) {
			throw new RuntimeException("修改失败");
		}
		
		resultBean.setSuccess(true);
		return resultBean;
	}
	
	public ResultBean update(List<EquipRepairRepository> objs) {
		ResultBean resultBean=new ResultBean(false);
		List<EquipRepairRepositoryEntity> entityList = new ArrayList<>();
		for(EquipRepairRepository obj:objs) {
			EquipRepairRepositoryEntity entity = obj.getInnerData();
			long id = entity.getId();
			long equipCategoryId = entity.getEquipCategoryId();
			long faultCauseId = entity.getFaultCauseId();
			//String faultCauses = entity.getFaultCauses();
			EquipRepairRepositoryEntity existsEntity = equipRepairRepositoryDao.selectById(id);
			if(existsEntity==null) {
				throw new RuntimeException("数据为空");
			}
			QueryWrapper<EquipRepairRepositoryEntity> queryWrapper = new QueryWrapper<EquipRepairRepositoryEntity>();
			queryWrapper.eq("equip_category_id", equipCategoryId);
		//	queryWrapper.eq("fault_causes", faultCauses);
			queryWrapper.eq("fault_cause_id", faultCauseId);
			queryWrapper.ne("equip_repair_repository_id", id);
			List<EquipRepairRepositoryEntity> existsEntitys = equipRepairRepositoryDao.selectList(queryWrapper);
			
			if(existsEntitys!=null && existsEntitys.size()>0){
				throw new RuntimeException("维修知识库已经存在");
			}
			baseEntityDataUtil.updateBaseEntityData(entity);
			entityList.add(entity);
		}
		
		
		boolean save = this.updateBatchById(entityList);
		if(!save) {
			throw new RuntimeException("修改失败");
		}
		
		resultBean.setSuccess(true);
		return resultBean;
	}
	
	public ResultBean delete(EquipRepairRepository obj) {
		ResultBean resultBean=new ResultBean(false);
		EquipRepairRepositoryEntity entity = obj.getInnerData();
		
		long id = entity.getId();
		EquipRepairRepositoryEntity existsEntity = equipRepairRepositoryDao.selectById(id);
		if(existsEntity==null) {
			throw new RuntimeException("数据为空");
		}
		
		
		
		
		baseEntityDataUtil.updateBaseEntityData(entity);
		entity.setRowStatus(0);
		boolean save = this.updateById(entity);
		if(!save) {
			throw new RuntimeException("删除失败");
		}
		
		resultBean.setSuccess(true);
		return resultBean;
	}
	
	public ResultBean delete(List<EquipRepairRepository> objs) {
		ResultBean resultBean=new ResultBean(false);
		List<EquipRepairRepositoryEntity> entityList = new ArrayList<>();
		for(EquipRepairRepository obj:objs) {
			EquipRepairRepositoryEntity entity = obj.getInnerData();
			long id = entity.getId();
			EquipRepairRepositoryEntity existsEntity = equipRepairRepositoryDao.selectById(id);
			if(existsEntity==null) {
				throw new RuntimeException("数据为空");
			}
			baseEntityDataUtil.updateBaseEntityData(entity);
			entity.setRowStatus(0);
			entityList.add(entity);
		}
		
		boolean save = this.updateBatchById(entityList);
		if(!save) {
			throw new RuntimeException("删除失败");
		}
		
		resultBean.setSuccess(true);
		return resultBean;
	}

	public EquipRepairRepository getById(long id) {
		EquipRepairRepositoryEntity byId = super.getById(id);
		if(byId!=null) {
			EquipRepairRepository equipRepairRepository = new EquipRepairRepository();
			equipRepairRepository.setInnerData(byId);
			return equipRepairRepository;
		}
		return null;
	}
	
	
}
